<template>
	<el-dialog title="修改服务路由配置" :visible.sync="dialogVisible" width="800px" @close="handleClose"
		:close-on-click-modal="false"
		:append-to-body="true">
		<div slot="title" class="dialog-header-title">
			<i class="el-icon-edit-outline"></i>
			<span>修改服务路由配置</span>
		</div>

		<div class="smp-search" v-if="dialogVisible">
			<el-form :inline="false" label-width="150px" size="mini" ref="form" :model="form" :rules="rules">

				<el-row>
					
					<el-col :span="24">
						<el-form-item label="服务系统:" prop="systemNo">
							<el-select v-model="form.systemNo" style="width: 100%" filterable remote reserve-keyword
								placeholder="请输入系统编号或名称" :remote-method="getsystems"  disabled>
								<el-option v-for="item in systemListAll" :key="item.systemNo" :label="item.systemName"
									:value="item.systemNo">
									<span style="float: left;margin-left: 3px;">{{ item.systemNo }}</span>
									<span style="float: right;margin-right: 3px;">{{ item.systemName }}</span>
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					
					<el-col :span="24">
						<el-form-item label="服务编码(R值):" prop="busCode">
							<el-input v-model="form.busCode" disabled placeholder="服务编码(R值)"></el-input>
						</el-form-item>
					</el-col>
					
					<el-col :span="22">
						<el-form-item label="ESB业务路由:" prop="esbBusPath">
							<el-select v-model="form.esbBusPath" style="width: 100%" filterable remote reserve-keyword
								placeholder="请输入代理路径"
								:loading="proLoading"
								@change="proChange"
								:remote-method="getBusPathList" >
								<el-option v-for="item in busPathList" :key="item.sourcePath" :label="item.sourcePath" :value="item.sourcePath">
									<span style="float: left;margin-left: 3px;">{{ item.sourceName }}</span>
									<span style="float: right;margin-right: 3px;">{{ item.sourcePath }}</span>
								</el-option>
							</el-select>
							
						<!-- 	<el-input v-model="form.esbBusPath" placeholder="ESB业务路由"></el-input>
							 -->
						</el-form-item>
					</el-col>
					
					<el-col :span="2" >
						<el-button size="mini" plain type="primary"  @click="addbusPath" > 创建 </el-button>
					</el-col>
					
					<el-col :span="22">
						<el-form-item label="OESB业务路由:" prop="oesbBusPath">
							
							<el-select v-model="form.oesbBusPath" style="width: 100%" filterable remote reserve-keyword
								placeholder="请输入代理路径"
								:loading="proLoading"
								@change="proChange"
								:remote-method="getoesbBusPathList" >
								<el-option v-for="item in oesbbusPathList" :key="item.sourcePath" :label="item.sourcePath" :value="item.sourcePath">
									<span style="float: left;margin-left: 3px;">{{ item.sourceName }}</span>
									<span style="float: right;margin-right: 3px;">{{ item.sourcePath }}</span>
								</el-option>
							</el-select>
							
							
							<!-- <el-input v-model="form.oesbBusPath" placeholder="OESB业务路由"></el-input> -->
						</el-form-item>
					</el-col>
					
					<el-col :span="2" >
						<el-button size="mini" plain type="primary"  @click="addoesbbusPath" > 创建 </el-button>
					</el-col>
					
					<el-col :span="24">
						<el-form-item label="消费编码(P值):" prop="pubCode">
							<el-input v-model="form.pubCode" placeholder="消费编码(P值)" disabled>
							</el-input>
						</el-form-item>
					</el-col>
					
					
					
					
					<el-col :span="24">
						<el-form-item label="报文协议(消费方):" prop="msgAgreement">
							<el-autocomplete placeholder="请选择或输入报文协议"  style="width: 100%;" :fetch-suggestions="fetchProtocal" v-model="form.msgAgreement" class="inline-input"></el-autocomplete>
						</el-form-item>
					</el-col>

						<!-- <el-col :span="24">
							<el-form-item label="报文编码:" prop="msgCode">
								<el-select v-model="form.msgCode" style="width: 100%" placeholder="报文编码">
									<el-option v-for="item in dict('SMP0004')" :key="item.itemCode" :label="item.itemName"
										:value="item.itemCode">
									</el-option>
								</el-select>
							</el-form-item>
						</el-col> -->
				</el-row>
			</el-form>
		</div>
		<span slot="footer" class="dialog-footer">
			<el-button size="mini"   type="primary" @click="onSubmit">确定</el-button>
			<el-button size="mini"  @click="closeDialog">取消</el-button>
		</span>
		
		<addESBSource ref="addESBSource" @changebusPath="changebusPath" @changeoesbbusPath="changeoesbbusPath"  />
		
	</el-dialog>
</template>

<script>
	"use strict";
	import { addConsumeInfo, autoGeneratePubCode} from '@/api/consume.js';
	import {getproList,getproListBysourcePath} from '@/api/source.js';
	import addESBSource from '@/view/processCommon/components/addESBSource';
	import {
			serviceUpdate
		} from '@/api/service.js';
	export default {
		
		data() {
			return {
				dialogVisible: false,
				rules: {
					pubCode: [{
						required: true,
						message: "消费编码(P值)不能为空",
						trigger: ['blur', 'change']
					}],
					esbBusPath: [{
						required: true,
						message: "ESB业务路由不能为空",
						trigger: ['blur', 'change']
					}],
					oesbBusPath: [{
						required: true,
						message: "ESB业务路由不能为空",
						trigger: ['blur', 'change']
					}],
					msgAgreement: [{
						required: true,
						message: "报文协议不能为空",
						trigger: ['blur', 'change']
					}],
					msgCode: [{
						required: true,
						message: "报文编码不能为空",
						trigger: ['blur', 'change']
					}]
				},
				sourceFolder:'',
				busPathList:[],
				oesbbusPathList:[],
				form: {
					busCode: '',
					pubCode: '',
					systemNo: '',
					esbBusPath: '',
					oesbBusPath: '',
					msgAgreement: '',
					msgCode: ''
				},

			};
		},
components: {
			
			addESBSource
		},

		methods: {
			
			onSubmit() {
				this.serviceUpdate();
			},
			
			/* autoGenerate(){
				this.autoGeneratePubCode();
			}, */
			
			serviceUpdate() {
							this.$refs["form"].validate(valid => {
								if (valid) {
									serviceUpdate(this.form).then(response => {
										/* this.data = response.data.extend;
										this.$message({
						duration:5000,
											type: 'success',
											message: '添加服务调用成功！'
										});
										this.form = {};
										this.$refs["form"].resetFields(); */
										this.$message({
						duration:5000,											type: 'success',
											message: '修改业务路由信息成功！'
										});
										this.dialogVisible = false;
										this.$emit("getInit");
									}).catch(error => {
										this.$message({
						duration:5000,										type: 'error',
											message: '修改业务路由信息失败！'
										});
										this.dialogVisible = false;
										this.$emit("getInit");
									});
								}
							})
			
						},
				
			addConsumeInfo() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						addConsumeInfo(this.form).then(response => {
							/* this.data = response.data.extend; */
							this.$message({
						duration:5000,						type: 'success',
								message: '调用关系新增成功！'
							});
							this.dialogVisible = false;
							this.$emit("changePubCodes",this.form.pubCode);
							this.reset();
						}).catch(error => {
							this.$message({
						duration:5000,					type: 'error',
								message: '调用关系新增失败！'
							});
							this.reset();
							this.dialogVisible = false;
						});
					}
				})

			},
			
			/* autoGeneratePubCode(){
				autoGeneratePubCode().then(response =>{
					this.form.pubCode = response.data.extend;
					this.this.$message({
						duration:5000,		type: 'success',
						message: '消费编码（P值）自动生成成功！'
					});
				}).catch(error =>{
					this.$this.$message({
						duration:5000,	type: 'error',
						message: '消费编码（P值）自动生成失败！'
					});
				})
			}, */
			
			showDialog(row) {
				this.form.busCode = row.busCode;
				this.form.pubCode = row.pubCode;
				this.form.systemNo = row.systemNo;
				this.form.msgAgreement = row.msgAgreement;
				
				this.form.esbBusPath = row.esbBusPath;
				this.form.oesbBusPath = row.oesbBusPath;
				
				let system =  this.$store.getters.getSystem(this.form.systemNo);
				this.sourceFolder = system.busPath;
				this.dialogVisible = true;
			},

			closeDialog() {
				this.reset();
				this.dialogVisible = false;
			},

			handleClose() {
				this.reset();
				this.dialogVisible = false;
			},
			
			
			getBusPathList(query){
				if(query!=''){
					this.proLoading = true;
					getproList({"sourcePath":query,"sourceType":"03","esbFlg":"01"}).then(response => {
					this.busPathList = response.data.extend;
					this.proLoading = false;
				});
				}else{
					this.busPathList = [];
				}
			},
			
			getoesbBusPathList(query){
				if(query!=''){
					this.proLoading = true;
					getproList({"sourcePath":query,"sourceType":"03","esbFlg":"02"}).then(response => {
					this.oesbbusPathList = response.data.extend;
					this.proLoading = false;
				});
				}else{
					this.busPathList = [];
				}
			},
			
			addbusPath(){
				this.$refs.addESBSource.showDialog("03",this.sourceFolder,"01");
			},
			addoesbbusPath(){
				this.$refs.addESBSource.showDialog("03",this.sourceFolder,"02");
			},
			
			changebusPath(sourcePath){
				getproList({"sourcePath":sourcePath,"sourceType":"03","esbFlg":"01"}).then(response =>{
					this.busPathList = response.data.extend;
					this.form.esbBusPath = sourcePath
				}).catch(error => {
					console.info("失败");
					console.info(error.message);
				})
			},
			
			changeoesbbusPath(sourcePath){
				getproList({"sourcePath":sourcePath,"sourceType":"03","esbFlg":"02"}).then(response =>{
					this.oesbbusPathList = response.data.extend;
					this.form.oesbBusPath = sourcePath
				}).catch(error => {
					console.info("失败");
					console.info(error.message);
				})
			},


		},

	}
</script>

<style>
</style>
